package cn.com.xuan.web;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/base")
public class BaseController {
    @ResponseBody
    @RequestMapping("/test")
    public Object test(){
        return "cn.com.xuan.web.UserController.test";
    }

    /**
     * 创建索引
     */
    @ResponseBody
    @RequestMapping("/createIndex")
    public Object createIndex(){
        try {
            //索引库存放的位置，也可以放在硬盘
            Directory directory= FSDirectory.open(new File("C:\\Users\\ZGJTYF\\Desktop\\lucene\\index"));
            //标准的分词器
//            Analyzer analyzer =new StandardAnalyzer();
            //  中文分词器
            Analyzer analyzer =new IKAnalyzer();
            //创建输出流write
            IndexWriterConfig config =new IndexWriterConfig(Version.LUCENE_4_10_2,analyzer);
            IndexWriter indexWriter = new IndexWriter(directory,config);


            //创建Filed域
            File f = new File("C:\\Users\\ZGJTYF\\Desktop\\lucene\\Filed");
            //找到下面的所有待搜索的文件
            File[] listFiles = f.listFiles();
            for (File file:listFiles){
                //创建文档对象
                Document document=new Document();
                //文件名称
                String file_name=file.getName();
                Field fileNameFiled=new TextField("fileName",file_name, Field.Store.YES);
                //文件大小
                long file_size= FileUtils.sizeOf(file);
                Field fileSizeField=new LongField("fileSize",file_size,Field.Store.YES);
                //文件路径
                String file_path=file.getPath();
                Field filePathField=new StoredField("filePath",file_path);
                //文件内容
                String file_content = FileUtils.readFileToString(file,"utf8");
                Field fileContentField=new TextField("fileContent",file_content, Field.Store.YES);

                //保存到文件对象里
                document.add(fileNameFiled);
                document.add(fileSizeField);
                document.add(filePathField);
                document.add(fileContentField);

                //写到索引库
                indexWriter.addDocument(document);
            }
            //关闭
            indexWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "true";
    }

    /**
     * 查询索引
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/searchIndex/{fld}/{txt}")
    public Object searchIndex(@PathVariable("fld") String fld,@PathVariable("txt") String txt) throws Exception{
        //第一步，查询准备工作，创建Directory对象
        Directory dir = FSDirectory.open(new File("C:\\Users\\ZGJTYF\\Desktop\\lucene\\index"));
        //创建IndexReader对象
        IndexReader reader= DirectoryReader.open(dir);
        //创建IndexSearch对象
        IndexSearcher search =new IndexSearcher(reader);

        //第二步，闯将查询条件对象
//        TermQuery query=new TermQuery(new Term("fileContent","what"));
        TermQuery query=new TermQuery(new Term(fld,txt));
        //第三步：执行查询，参数（1：查询条件对象，2：查询结果返回的最大值）
        TopDocs topDocs=search.search(query,10);
        //第四步：处理查询结果
        //输出结果数量
        System.out.print("查询结果数量："+topDocs.totalHits);
        //取得结果集
        ScoreDoc[] scoreDocs=topDocs.scoreDocs;
        for (ScoreDoc scoreDoc:scoreDocs){
            System.out.println("当前doc得分:"+scoreDoc.score);
            //根据文档对象ID取得文档对象
            Document doc=search.doc(scoreDoc.doc);
            System.out.println("文件名称："+doc.get("fileName"));
            System.out.println("文件路径："+doc.get("filePath"));
            System.out.println("文件大小："+doc.get("fileSize"));
            System.out.println("=======================================");
        }
        //关闭IndexReader对象
        reader.close();

        return scoreDocs;
    }
}
